﻿//逻辑代码
import {Puerts, ScreenMgr, UnityEngine} from 'csharp'
import {Component} from "framework/component/component";
import {ServiceLocator} from "framework/decorator/serviceLocator";
import {TimeUtil} from "framework/util/TimeUtil";
import {$typeof} from 'puerts'
import {Empty} from "ui/Empty";
import SimpleTweenScreen = ScreenMgr.SimpleTweenScreen;
import Button = UnityEngine.UI.Button;
import ScreenManager = ScreenMgr.ScreenManager;
import Text = UnityEngine.UI.Text;
import Vector3 = UnityEngine.Vector3;
import Time = UnityEngine.Time;
import Image = UnityEngine.UI.Image;
import GameObject = UnityEngine.GameObject;

class Popup  extends Component
{
    arg0:Button = null;
    Adder:Button = null
    message:Text
    constructor(behaviour:Puerts.JsBehaviour) {
        super(behaviour);
        this.applyArgs()
    }
    //Awake(){
    //  console.log('js Awake ...')
    // }
    Start (){
        this.transform.localPosition = Vector3.zero;
        console.log("pop start,.....")
    }
    OnEnable(){
        // @ts-ignore
        this.message.text = this.behaviour.TransitionData;
        this.arg0.onClick.AddListener(()=>{
            ServiceLocator.Get(ScreenManager).Hide()
            console.log('onclick ok,hide')
        })
        this.Adder.onClick.AddListener(()=>{
            let e = this.GetComponent(Empty)
            if(e)
                GameObject.DestroyImmediate(e.behaviour)
            this.AddComponent(Empty,"ui/Empty")
        })
         console.log('js OnEnable ...')
    }
    OnDisable(){
         console.log('js OnDisable ...')
        // @ts-ignore
        this.arg0.onClick.RemoveAllListeners()
        // @ts-ignore
        this.Adder.onClick.RemoveAllListeners()
    }
    // LateUpdate(){//注册到componentmgr的事件        
    // }
    //FixedUpdate(){//注册到componentmgr的事件
    //}
    tick:number=0
    Update() //注册到componentmgr的事件
    {
        //js不支持操作符重载所以Vector3的乘这么用
        // @ts-ignore
        if(this.tick ++ > 15) {
            // @ts-ignore
            this.message.text = this.behaviour.TransitionData + TimeUtil.GetTimeStringHMS(Time.realtimeSinceStartup);
            this.tick=0;
        }
    }
    public OnDestroy() {
        console.log('Popup OnDestroy...');
    }
}

export function init (jsBehavior:Puerts.JsBehaviour) {
    new Popup(jsBehavior);
}
